package cn.wom.core.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.wom.core.bean.User;

public class DefaultInterceptor implements HandlerInterceptor {
	@Override
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		User user = (User) request.getSession().getAttribute("user_login");
		String requestURI = request.getRequestURI();
		if(user==null){
		
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			return false;
		}
		if(requestURI.contains("/queryUser")){
			if(user.getRole().equals("manager")){
				return true;
			}
			else{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write("权限不足,禁止操作");
				return false;
			}
		}
		if(requestURI.contains("/updateUser")){
			if(user.getRole().equals("manager")){
				return true;
			}
			else{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write("权限不足,禁止操作");
				return false;
			}
		}
		if(requestURI.contains("/deleteUser")){
			if(user.getRole().equals("manager")){
				return true;
			}
			else{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write("权限不足,禁止操作");
				return false;
			}
		}
		if(requestURI.contains("/register")){
			if(user.getRole().equals("manager")){
				return true;
			}
			else{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write("权限不足,禁止操作");
				return false;
			}
		}
		if(requestURI.contains("/addMissionOrder")){
			if(user.getRole().equals("manager")){
				return true;
			}
			else{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write("权限不足,禁止操作");
				return false;
			}
		}
		if(requestURI.contains("/deleteMissionOrder")){
			if(user.getRole().equals("manager")){
				return true;
			}
			else{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write("权限不足,禁止操作");
				return false;
			}
		}
		if(requestURI.contains("/updateMissionOrder")){
			if(user.getRole().equals("manager")){
				return true;
			}
			else{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write("权限不足,禁止操作");
				return false;
			}
		}
		return true;
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}


}
